import { Injectable } from '@angular/core';

import {Observable} from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class RequestService {

  constructor() { }


  //同步方法
  getData() {
    return 'this is a service data';
  }

  //回调函数
  getCallBackData(cb:any) {
    setTimeout(() => {
      let username = '张三';
      
      // return username;
      cb(username);
    }, 1000);

  }

  //Promise
  getPromiseData() {
    return new Promise((resolve,reject) => {

      setTimeout(() => {
        let username = '张三--Promise';
        
        resolve(username)

      }, 3000);
    })
  }

  //RxJS
  getRxJSData() {
    return new Observable((observer) =>{
      setTimeout(() => {
        let username = '张三--RxJS';
      
      observer.next(username)
      //  observer.error('失败数据');

      }, 3000);
    })
  }


  // RxJS执行多次
  getRxJSIntervalData() {
    let count=0;
    return new Observable((observer) =>{
      setInterval(()=>{
        count++;
        let username='张三执行多次'+count;
        observer.next(username)
      },1000);
    })
  }

  //用工具方法对返回的数据进行处理
  getRxJSIntervalNum() {
    let count=0;
    return new Observable((observer) =>{
      setInterval(()=>{
        count++;
        observer.next(count)
      },1000);
    })
  }

}
